package cn.kgc.day05.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author 课工场
 * @date 2024/10/11
 * @description
 */
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置参数的编码格式   解决中文乱码   应该在获取请求参数之前完成
        req.setCharacterEncoding("UTF-8");

        // 1. 获取方法名
        String method = req.getParameter("method");

        // 2. 根据方法名获取方法对象
        try {
            Method declaredMethod = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(this,req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
